//
//  MemberHeaderFooterView.swift
//  TinyShopKeeper
//
//  Created by huangtao on 16/6/1.
//  Copyright © 2016年 fish. All rights reserved.
//

import UIKit


protocol HeaderFooterViewDelegate
{
    //默认 必须实现的
    func didSelect(headerFooterView:MemberHeaderFooterView)
}

class MemberHeaderFooterView: UITableViewHeaderFooterView {
    
    var section:Int = 0//表示headerview的section
    
    let orderView:OrderView
    var delegate:HeaderFooterViewDelegate?
    var selected:Bool = false//表示对应的section 有没有展开，初始为false，并且附新数据时置为false
    var orderDataource:[String:AnyObject] = [String:AnyObject] (){
        didSet{
            
            self .loadData()
        }
    }
    
    func loadData()
    {
        //赋值
        //取到的值可能是string也有可能是NSNumber 须转化为String 再赋值
        self.orderView.deskNoLabel.text = self.orderDataource["order_stable_deskno"] as? String
        self.orderView.personNumberLabel.text = self.orderDataource["order_person_num"] as? String
        self.orderView.time_length_label.text = self.orderDataource["time_length"] as? String
        self.orderView.order_goods_amount_label.text = self.orderDataource["order_order_amount"] as? String
        self.orderView.order_order_amount_label.text = self.orderDataource["order_goods_amount"] as? String
        self.orderView.order_cash_money_label.text = self.orderDataource["order_cash_money"] as? String
        self.orderView.order_creditcard_money_label.text = self.orderDataource["order_creditcard_money"] as? String
        self.orderView.order_use_balance_label.text = self.orderDataource["order_use_balance"] as? String
        self.orderView.shopdt_discount_label.text = self.orderDataource["shopdt_discount"] as? String
        self.orderView.orderLabel.text = self.orderDataource["order_sn"] as? String
        self.orderView.order_waiter_account_label.text = self.orderDataource["order_waiter_account"] as? String
        self.orderView.order_shopping_volume_fee_label.text = self.orderDataource["order_shopping_volume_fee"] as? String
        
        //订单号时间 2016-04-08 14:01:12
        let timeStr = self.orderDataource["order_shipping_time"] as? NSString
        
        let timeLableStr = timeStr!.substringFromIndex(11)
        let dayLableStr = timeStr!.substringWithRange(NSMakeRange(8, 2))+"日"
        let yearLableStr = timeStr!.substringToIndex(7)
        //时分秒
        self.orderView.timeLabel.text = timeLableStr
        //年月
        self.orderView.yearMonthLabel.text = yearLableStr
        //天
        self.orderView.dayLabel.text = dayLableStr
        
        //        self.orderView.timeLabel.text = self.orderDataource["order_shipping_time"] as? String
//        print(orderDataource)
        
        
        //button 点击事件
        self.orderView.tapButton .addTarget(self, action: "taped", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.orderView.tapButton.selected = false
        
        //每次赋值数据 默认收起
        self.selected = false
        
    }
    
//    func loadData()
//    {
//        //加载数据
////        self.orderView.deskNoLabel.text = self.orderDataource[""] as? String//有可能是String也有可能是NSNumber 需转为String再赋值
//        
//        
//        //button 点击事件
//        self.orderView.tapButton .addTarget(self, action: "taped", forControlEvents: UIControlEvents.TouchUpInside)
//        self.orderView.tapButton.selected = false
//        
//    }
    //按钮被点击
    func taped()
    {
        //改变状态
        self.selected = !self.selected
        //重新加载对应某个section的数据 包括cell的数据
        //tableview的代理为controller，需要通知controller，让它去实现刷新数据
        //使用代理传值~就是protocol HeaderFooterViewDelegate
        
        if (self.delegate != nil){
            self.delegate?.didSelect(self)
        }
    }

   
    //MARK:---init
    
    override init(reuseIdentifier: String?) {
  
    let view = NSBundle.mainBundle().loadNibNamed("OrderView", owner: nil, options: nil).first as! OrderView
    view.frame = CGRectMake(0, 0, ScreenWidth*0, 0)
    self.orderView = view
    super.init(reuseIdentifier: reuseIdentifier!)
   self .addSubview(view)
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
  
}
